package generator;

import java.awt.Point;
import java.util.Random;

import map.Map;
import map.Texture;
import mission.Mission;
import mission.MissionStep;
import mission.MissionStep.MissionType;

import utilities.WeightedRandom;


import controller.LocalManager;

public class MissionGenerator {
	
	public static Mission next(){
		Map map = LocalManager.getMap();
		int x = LocalManager.getPlayerLocation().x;
		int y = LocalManager.getPlayerLocation().y;
		WeightedRandom num = new WeightedRandom(0.0, 20.0, 40.0, 40.0);
		int stepNumber = num.next();
		
		Random r = new Random();
		int distance = (stepNumber*2+1);
		int addX = (int)((r.nextDouble()*2 - 1)*distance);
		int addY = (int)Math.sqrt(distance*distance - addX*addX);
		addY = r.nextBoolean()? addY : -addY;
		Texture t = map.getTexture(x+addX, y+addY);
		while(t == null || t == Texture.WATER){
			addX = (int)((r.nextDouble()*2 - 1)*distance);
			addY = (int)Math.sqrt(distance*distance - addX*addX);
			addY = r.nextBoolean()? addY : -addY;
			t = map.getTexture(x+addX, y+addY);
		}

		MissionStep finish = new MissionStep(new Point(x+addX, y+addY), MissionType.COMBAT);
		
		MissionStep temp = finish;
		for(int i = stepNumber; i>0; i--){
			addX = addX*i/(i+1);
			addY = addY*i/(i+1);
			MissionStep step = new MissionStep(new Point(x+addX, y+addY), MissionType.COMBAT);
			step.addNextStep(temp);
			temp = step;
		}
		
		return new Mission(new StringGenerator().next(), temp, finish);
	}
}
